/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.serverpackets;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.echo.OpcodesServer;

public abstract class ServerBasePacket extends OpcodesServer {
	private static Logger _log = Logger.getLogger(ServerBasePacket.class.getName());

	private static final String CLIENT_LANGUAGE_CODE = Config.CLIENT_LANGUAGE_CODE;

	ByteArrayOutputStream _bao = new ByteArrayOutputStream();

	public byte[] getBytes() {
		try {
			final int padding = this._bao.size() % 4;

			if (padding != 0) {
				for (int i = padding; i < 4; i++) {
					this.writeC(0x00);
				}
			}
		} catch (final Exception e) {
		}
		return this._bao.toByteArray();
	}

	public abstract byte[] getContent() throws IOException;

	public int getLength() {
		return this._bao.size() + 2;
	}

	/**
	 * サーバーパケットの種類を表す文字列を返す。("[S] S_WhoAmount" 等)
	 */
	public String getType() {
		return "[S] " + this.getClass().getSimpleName();
	}

	protected void writeByte(final byte[] text) {
		try {
			if (text != null) {
				this._bao.write(text);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	protected void writeC(final int value) {
		try {
			this._bao.write(value & 0xff);
		} catch (final Exception e) {
		}
	}

	protected void writeD(final int value) {
		try {
			this._bao.write(value & 0xff);
			this._bao.write(value >> 8 & 0xff);
			this._bao.write(value >> 16 & 0xff);
			this._bao.write(value >> 24 & 0xff);
		} catch (final Exception e) {
		}
	}

	protected void writeExp(final long value) {
		try {
			this._bao.write((int) (value & 0xff));
			this._bao.write((int) (value >> 8 & 0xff));
			this._bao.write((int) (value >> 16 & 0xff));
			this._bao.write((int) (value >> 24 & 0xff));

			/*
			 * this._bao.write((int) (value & 0xff)); this._bao.write((int)
			 * (value >> 8 & 0xff)); this._bao.write((int) (value >> 16 &
			 * 0xff)); this._bao.write((int) (value >> 24 & 0xff));
			 * this._bao.write((int) (value >> 32 & 0xff));
			 * this._bao.write((int) (value >> 40 & 0xff));
			 * this._bao.write((int) (value >> 48 & 0xff));
			 * this._bao.write((int) (value >> 56 & 0xff));
			 */
		} catch (final Exception e) {
		}
	}

	protected void writeF(final double org) {
		try {
			final long value = Double.doubleToRawLongBits(org);
			this._bao.write((int) (value & 0xff));
			this._bao.write((int) (value >> 8 & 0xff));
			this._bao.write((int) (value >> 16 & 0xff));
			this._bao.write((int) (value >> 24 & 0xff));
			this._bao.write((int) (value >> 32 & 0xff));
			this._bao.write((int) (value >> 40 & 0xff));
			this._bao.write((int) (value >> 48 & 0xff));
			this._bao.write((int) (value >> 56 & 0xff));
		} catch (final Exception e) {
		}
	}

	protected void writeH(final int value) {
		try {
			this._bao.write(value & 0xff);
			this._bao.write(value >> 8 & 0xff);
		} catch (final Exception e) {
		}
	}

	protected void writeL(final long value) {
		try {
			this._bao.write((int) (value & 0xff));
		} catch (final Exception e) {
		}
	}

	protected void writeP(final int value) {
		try {
			this._bao.write(value);
		} catch (final Exception e) {
		}
	}

	protected void writeS(final String text) {
		try {
			if (text != null) {
				this._bao.write(text.getBytes(CLIENT_LANGUAGE_CODE));
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}

		this._bao.write(0);
	}
}
